package com.cn.longdistancebusstation.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.adapter.ComplishAdapter;
import com.cn.longdistancebusstation.annotation.OrderStatus;
import com.cn.longdistancebusstation.calendar.CalendarActivity;
import com.cn.longdistancebusstation.base.BaseFragment;
import com.cn.longdistancebusstation.callback.OnCustomViewClickListener;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.ListResult;
import com.cn.longdistancebusstation.model.OrderInfo;
import com.cn.longdistancebusstation.order.OrderDetailActivity;
import com.cn.longdistancebusstation.view.ClearEditText;
import com.cn.longdistancebusstation.view.UncomplishListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2018/1/16.
 */

public class UncomplishFragment extends BaseFragment implements View.OnClickListener,AdapterView.OnItemClickListener, OnCustomViewClickListener{
    private UncomplishListView listView;
    private ComplishAdapter adapter;
    private ClearEditText mDateEditText;
    private String mOutingDate;
    private String order_date;
    private int order_state=0;
    private String id;
    private int page=1;
    private int rows=100;
    private ArrayList<OrderInfo> mOrderInfoList =new ArrayList<>();
    private String time;
    private ScrollView mLinearLayout;
    private RelativeLayout mRelativeLayout;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_uncomplish,null);
        mLinearLayout=(ScrollView)view.findViewById(R.id.main_layout);
        mRelativeLayout=(RelativeLayout)view.findViewById(R.id.child_layout);
        mDateEditText =(ClearEditText) view.findViewById(R.id.select_time);
        mDateEditText.requestFocus();
        mDateEditText.setInputType(InputType.TYPE_NULL);
        mDateEditText.setOnClickListener(this);
        mDateEditText.setOnCustomViewClickListener(this);
        listView=(UncomplishListView)view.findViewById(R.id.uncomplish);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("orderNo", mOrderInfoList.get(i).getOrderNo());
                intent.putExtra("sendDate", mOrderInfoList.get(i).getSendDate());
                intent.putExtra("orderStatus", OrderStatus.ORDER_UNFINISH);
                startActivity(intent);
            }
        });

        getOrderList();
        return view;
    }

    private void getOrderList() {
        mOrderInfoList.clear();
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<ListResult<OrderInfo>> call=httpService.getOrderList(mOutingDate, GlobalVariable.getUserID(),page,rows,0);
        call.enqueue(new Callback<ListResult<OrderInfo>>() {
            @Override
            public void onResponse(Call<ListResult<OrderInfo>> call, Response<ListResult<OrderInfo>> response) {
                hideHud();
                ListResult result = response.body();
                Gson gson=new Gson();
                String strBody=gson.toJson(result.getBody().getData());
                Type type=new TypeToken<List<OrderInfo>>(){}.getType();
                mOrderInfoList =gson.fromJson(strBody, type);
                for(int i = 0; i< mOrderInfoList.size(); i++){
                    String order_number= mOrderInfoList.get(i).getOrderNo();
                    String order_time= mOrderInfoList.get(i).getOrderDate();
                    String order_last_pay_time= mOrderInfoList.get(i).getLatestPayDate();
                    String order_id= mOrderInfoList.get(i).getOrderID();
                    String arrive_name= mOrderInfoList.get(i).getToStationName();
                    String bus_number= mOrderInfoList.get(i).getBusNo();

                }
                adapter=new ComplishAdapter(getActivity(), mOrderInfoList);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                if(mOrderInfoList.size()==0){
                    mLinearLayout.setVisibility(View.GONE);
                    mRelativeLayout.setVisibility(View.VISIBLE);
                }else{
                    mLinearLayout.setVisibility(View.VISIBLE);
                    mRelativeLayout.setVisibility(View.GONE);

                }
            }

            @Override
            public void onFailure(Call<ListResult<OrderInfo>> call, Throwable t) {
                hideHud();
                Toast.makeText(getActivity(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.select_time:
                Date date = new Date();
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Intent intent = new Intent(getActivity(), CalendarActivity.class);
                intent.putExtra("outingDate", dateFormat.format(date));
                startActivityForResult(intent,1);
                break;

        }
    }

    @Override
    /*
    * 点击了EditText的清除按钮
    * */
    public void onClick() {
        mOutingDate = "";
        getOrderList();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            if(resultCode == RESULT_OK){
                mOutingDate = data.getStringExtra("outingDate");
                mDateEditText.setText(mOutingDate);
                getOrderList();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getOrderList();
    }
}
